#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

srcdir=VTK-${pkgver}

extra_args=()
if [ "${arch}" != "${host_arch}" ]; then
    extra_args+=(-DCMAKE_SYSTEM_NAME=Linux) # cross-compiling
    extra_args+=(-DCMAKE_SYSTEM_PROCESSOR=${arch})

    extra_args+=(-DEXE_SQLITE3=/host/usr/bin/sqlite3)

    extra_args+=(-DCMAKE_REQUIRE_LARGE_FILE_SUPPORT=FAILED_TO_RUN)
    extra_args+=(-DCMAKE_REQUIRE_LARGE_FILE_SUPPORT__TRYRUN_OUTPUT="")
    extra_args+=(-DVTK_REQUIRE_LARGE_FILE_SUPPORT_EXITCODE=FAILED_TO_RUN)
    extra_args+=(-DVTK_REQUIRE_LARGE_FILE_SUPPORT_EXITCODE__TRYRUN_OUTPUT_STDOUT="")
    extra_args+=(-DVTK_REQUIRE_LARGE_FILE_SUPPORT_EXITCODE__TRYRUN_OUTPUT_STDERR="")
    extra_args+=(-DTEST_LFS_WORKS_RUN=FAILED_TO_RUN)
    extra_args+=(-DTEST_LFS_WORKS_RUN__TRYRUN_OUTPUT_STDOUT="")
    extra_args+=(-DTEST_LFS_WORKS_RUN__TRYRUN_OUTPUT_STDERR="")
    extra_args+=(-DH5_PRINTF_LL_TEST_RUN=FAILED_TO_RUN)
    extra_args+=(-DH5_PRINTF_LL_TEST_RUN__TRYRUN_OUTPUT="")
    extra_args+=(-DH5_LDOUBLE_TO_LONG_SPECIAL_RUN=FAILED_TO_RUN)
    extra_args+=(-DH5_LDOUBLE_TO_LONG_SPECIAL_RUN__TRYRUN_OUTPUT="")
    extra_args+=(-DRUN_RESULT_VAR=FAILED_TO_RUN)
    extra_args+=(-DRUN_RESULT_VAR__TRYRUN_OUTPUT="")
    extra_args+=(-DH5_LONG_TO_LDOUBLE_SPECIAL_RUN=FAILED_TO_RUN)
    extra_args+=(-DH5_LONG_TO_LDOUBLE_SPECIAL_RUN__TRYRUN_OUTPUT="")
    extra_args+=(-DH5_LDOUBLE_TO_LLONG_ACCURATE_RUN=FAILED_TO_RUN)
    extra_args+=(-DH5_LDOUBLE_TO_LLONG_ACCURATE_RUN__TRYRUN_OUTPUT="")
    extra_args+=(-DH5_LLONG_TO_LDOUBLE_CORRECT_RUN=FAILED_TO_RUN)
    extra_args+=(-DH5_LLONG_TO_LDOUBLE_CORRECT_RUN__TRYRUN_OUTPUT="")
    extra_args+=(-DH5_DISABLE_SOME_LDOUBLE_CONV_RUN=FAILED_TO_RUN)
    extra_args+=(-DH5_DISABLE_SOME_LDOUBLE_CONV_RUN__TRYRUN_OUTPUT="")
    extra_args+=(-DH5_NO_ALIGNMENT_RESTRICTIONS_RUN=FAILED_TO_RUN)
    extra_args+=(-DH5_NO_ALIGNMENT_RESTRICTIONS_RUN__TRYRUN_OUTPUT="")
    extra_args+=(-DKWSYS_LFS_WORKS=FAILED_TO_RUN)
    extra_args+=(-DKWSYS_LFS_WORKS__TRYRUN_OUTPUT="")
else
    extra_args+=(-DEXE_SQLITE3=/usr/bin/sqlite3)
fi

if [ -n "${feature_standalone_compile_tools}" ]; then
    extra_args+=(-DVTK_BUILD_COMPILE_TOOLS_ONLY=ON)
fi

extra_args+=(-DSQLITE3_VERSION=${host_dep_sqlite_version})

cmake \
    -S ${srcdir} \
    -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=lib \
    ${extra_args[@]} \
    || exit 1
make -C build -j ${MAX_JOBS} || exit 1
